package jcu.salt;

import java.io.PrintStream;
import java.util.Date;

/** simple but convenient log facility */
public final class Logger {
	 
	/** create the instance */
    private static final Logger INSTANCE = new Logger();
    
    /** the target */
    private PrintStream out;

    /** restrict access */ 
    private Logger() {
    	out = null;
    }

    /** returns a logger object */
    public static Logger getInstance() {
        return INSTANCE;
    }
    
    /**
     * sets the output stream
     * @param out the target stream (null disables logging)
     */
    public void setOutput(PrintStream out) {
    	this.out = out;
    }
    
    /**
     * logs a given message with preceeding timestamp to the output stream 
     * if output stream is set
     * @param msg string to log
     */
    public static void log(String msg) {
    	PrintStream out = getInstance().out;
    	if(out != null) {
    		out.println(new Date() + " [" + System.currentTimeMillis() + "] " + msg);
    	}
    }

}

